<template>
  <div>
    <h1>权限管理</h1>
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>权限列表</span>
          <el-button type="primary" @click="showAddDialog = true" v-if="canAddPermission" :disabled="addFormRef?.validateField('name') || addFormRef?.validateField('description')">新增权限</el-button>
        </div>
      </template>
      <div class="card-content">
        <el-table :data="permissions" style="width: 100%">
          <el-table-column prop="name" label="权限名称" width="180" />
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作" width="180">
            <template #default="scope">
              <el-button size="small" @click="handleEdit(scope.row)" v-if="canEditPermission">编辑</el-button>
              <el-button size="small" type="danger" @click="handleDelete(scope.row)" v-if="canDeletePermission">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>

    <!-- 新增权限对话框 -->
    <el-dialog v-model="showAddDialog" title="新增权限" width="30%">
<el-form :model="newPermission" :rules="rules" ref="addFormRef" label-width="80px">
  <el-form-item label="权限名称" prop="name" required>
    <el-input v-model="newPermission.name" />
  </el-form-item>
  <el-form-item label="描述" prop="description">
    <el-input v-model="newPermission.description" type="textarea" />
  </el-form-item>
</el-form>
      <template #footer>
        <el-button @click="showAddDialog = false">取消</el-button>
        <el-button type="primary" @click="handleAdd" :disabled="addFormRef?.validateField('name') || addFormRef?.validateField('description')">确认</el-button>
      </template>
    </el-dialog>

    <!-- 编辑权限对话框 -->
    <el-dialog v-model="showEditDialog" title="编辑权限" width="30%">
<el-form :model="editingPermission" :rules="editRules" ref="editFormRef" label-width="80px">
  <el-form-item label="权限名称">
    <el-input v-model="editingPermission.name" disabled />
  </el-form-item>
  <el-form-item label="描述" prop="description">
    <el-input v-model="editingPermission.description" type="textarea" />
  </el-form-item>
</el-form>
      <template #footer>
        <el-button @click="showEditDialog = false">取消</el-button>
        <el-button type="primary" @click="handleUpdate" :disabled="editFormRef?.validateField('description')">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import api from '@/api'

const permissions = ref([])
const showAddDialog = ref(false)
const showEditDialog = ref(false)

const newPermission = ref({
  name: '',
  description: ''
})

const editingPermission = ref({
  id: null,
  name: '',
  description: ''
})

onMounted(async () => {
  await fetchPermissions()
})

const fetchPermissions = async () => {
  try {
    const response = await api.permission.getPermissions()
    permissions.value = response.data
  } catch (error) {
    ElMessage.error('获取权限列表失败')
    console.error(error)
  }
}

const handleEdit = (permission) => {
  editingPermission.value = {
    id: permission.id,
    name: permission.name,
    description: permission.description
  }
  showEditDialog.value = true
}

const handleUpdate = async () => {
  try {
    await api.permission.updatePermission(editingPermission.value.id, {
      description: editingPermission.value.description
    })
    await fetchPermissions()
    showEditDialog.value = false
    ElMessage.success('权限更新成功')
  } catch (error) {
    ElMessage.error(error.response?.data?.message || '更新权限失败')
  }
}

const handleDelete = async (permission) => {
  try {
    await ElMessageBox.confirm(
      `确定删除权限 ${permission.name}? 此操作不可撤销。`,
      '警告', 
      {
        type: 'warning',
        customClass: 'delete-confirm',
        center: true
      }
    )
    await api.permission.deletePermission(permission.id)
    await fetchPermissions()
    ElMessage.success('权限删除成功')
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除权限失败')
    }
  }
}

const handleAdd = async () => {
  try {
    await api.permission.createPermission(newPermission.value)
    await fetchPermissions()
    showAddDialog.value = false
    newPermission.value = { name: '', description: '' }
    ElMessage.success('权限创建成功')
  } catch (error) {
    ElMessage.error(error.response?.data?.message || '创建权限失败')
  }
}
</script>

<style scoped>
@import '@/assets/common.css';

.box-card {
  composes: common-card;
}
.card-header {
  composes: common-card-header;
}
.card-content {
  composes: common-card-content;
}
</style>
